package com.link.linkupjavaweb.controller.user;

import com.link.linkupjavaweb.common.Constants;
import com.link.linkupjavaweb.common.Result;
import com.link.linkupjavaweb.dto.ItemPurchaseDTO;
import com.link.linkupjavaweb.entity.User;
import com.link.linkupjavaweb.service.ItemService;
import com.link.linkupjavaweb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 道具商店Controller
 */
@Controller
@RequestMapping("/user/shop")
public class ItemShopController {
    
    @Autowired
    private ItemService itemService;
    
    @Autowired
    private UserService userService;
    
    /**
     * 道具商店页面
     */
    @GetMapping
    public String shopPage(HttpSession session, Model model) {
        User user = (User) session.getAttribute(Constants.SESSION_USER);
        if (user == null) {
            return "redirect:/user/login";
        }
        
        // 刷新用户信息（获取最新积分）
        User latestUser = userService.getUserById(user.getId());
        model.addAttribute("user", latestUser);
        
        return "user/shop/index";
    }
    
    /**
     * 获取商店道具列表
     */
    @GetMapping("/items")
    @ResponseBody
    public Result<Map<String, Object>> getShopItems(HttpSession session) {
        try {
            User user = (User) session.getAttribute(Constants.SESSION_USER);
            if (user == null) {
                return Result.error("请先登录");
            }
            
            Map<String, Object> shop = itemService.getItemShop();
            
            // 添加用户积分信息
            User latestUser = userService.getUserById(user.getId());
            shop.put("userScore", latestUser.getTotalScore());
            
            // 添加用户已拥有的道具数量
            Map<String, Integer> userItems = itemService.getUserItems(user.getId());
            shop.put("userItems", userItems);
            
            return Result.success(shop);
        } catch (Exception e) {
            return Result.error("获取商店信息失败：" + e.getMessage());
        }
    }
    
    /**
     * 购买道具
     */
    @PostMapping("/purchase")
    @ResponseBody
    public Result<Map<String, Object>> purchaseItem(@RequestBody ItemPurchaseDTO dto, HttpSession session) {
        try {
            User user = (User) session.getAttribute(Constants.SESSION_USER);
            if (user == null) {
                return Result.error("请先登录");
            }
            
            // 购买道具
            int newCount = itemService.purchaseItem(user.getId(), dto.getItemCode(), dto.getQuantity());
            
            // 获取最新用户信息
            User latestUser = userService.getUserById(user.getId());
            
            // 更新session
            session.setAttribute(Constants.SESSION_USER, latestUser);
            
            // 返回结果
            Map<String, Object> result = new HashMap<>();
            result.put("newCount", newCount);
            result.put("remainingScore", latestUser.getTotalScore());
            
            return Result.success("购买成功", result);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}

